Load packages and data

Load scripts and packages. Load the processed data we generated earlier.

library(MoleculeExperiment) # for data structure
library(SpatialExperiment) # for data structure
library(randomForest) # for predictive model
library(scater) # for plotting
library(patchwork) # for plotting
pancreas_colours = readRDS("../analysisOutput/pancreas_colours.Rds")
fit = readRDS("../analysisOutput/sc_cell_type_RF_fit.Rds")
feats = rownames(fit$importance)
me = readRDS("../processedData/xenium_pancreas_me.Rds")
me
## MoleculeExperiment class
## 
## molecules slot (1): detected
## - detected:
## samples (1): Xenium_V1_human_Pancreas_FFPE_outs
## -- Xenium_V1_human_Pancreas_FFPE_outs:
## ---- features (541): ABCC11 ACE2 ... VWA5A VWF
## ---- molecules (8073840)
## ---- location range: [3.1,7251.91] x [7.42,2920.68]
## 
## 
## boundaries slot (2): cell tiles
## - cell:
## samples (1): Xenium_V1_human_Pancreas_FFPE_outs
## -- Xenium_V1_human_Pancreas_FFPE_outs:
## ---- segments (140702): aaaadnje-1 aaacalai-1 ... oimaiaae-1 oimajkkk-1
## - tiles:
## samples (1): Xenium_V1_human_Pancreas_FFPE_outs
## -- Xenium_V1_human_Pancreas_FFPE_outs:
## ---- segments (95060): cell_1 cell_10 ... cell_9998 cell_9999

In this document we take the MoleculeExperiment object, summarise to SpatialExperiment and perform cell type prediction on both assays.

Count molecules over boundaries

We use the countMolecules function to count the molecules over the bounaries for either cells or tiles. This generates SpatialExperiment objects with different numbers of cells/tiles.

We also perform filtering and logcount normalisation.

spe_cells = countMolecules(me, boundariesAssay = "cell")[feats,]
spe_tiles = countMolecules(me, boundariesAssay = "tiles")[feats,]

spe_cells = addPerCellQCMetrics(spe_cells)
spe_tiles = addPerCellQCMetrics(spe_tiles)

spe_cells = logNormCounts(spe_cells[, spe_cells$total >= 5])
spe_tiles = logNormCounts(spe_tiles[, spe_tiles$total >= 5])

spe_cells
## class: SpatialExperiment 
## dim: 371 134610 
## metadata(0):
## assays(2): counts logcounts
## rownames(371): ABCC11 ACE2 ... VWA5A VWF
## rowData names(0):
## colnames(134610): Xenium_V1_human_Pancreas_FFPE_outs.aaaadnje-1
##   Xenium_V1_human_Pancreas_FFPE_outs.aaacalai-1 ...
##   Xenium_V1_human_Pancreas_FFPE_outs.oimaiaae-1
##   Xenium_V1_human_Pancreas_FFPE_outs.oimajkkk-1
## colData names(8): sample_id cell_id ... total sizeFactor
## reducedDimNames(1): spatial
## mainExpName: NULL
## altExpNames(0):
## spatialCoords names(2) : x_location y_location
## imgData names(1): sample_id
spe_tiles
## class: SpatialExperiment 
## dim: 371 82811 
## metadata(0):
## assays(2): counts logcounts
## rownames(371): ABCC11 ACE2 ... VWA5A VWF
## rowData names(0):
## colnames(82811): Xenium_V1_human_Pancreas_FFPE_outs.cell_10000
##   Xenium_V1_human_Pancreas_FFPE_outs.cell_10001 ...
##   Xenium_V1_human_Pancreas_FFPE_outs.cell_9998
##   Xenium_V1_human_Pancreas_FFPE_outs.cell_9999
## colData names(8): sample_id cell_id ... total sizeFactor
## reducedDimNames(1): spatial
## mainExpName: NULL
## altExpNames(0):
## spatialCoords names(2) : x_location y_location
## imgData names(1): sample_id

Predict cell types

pred_cells = predict(fit, newdata = as.matrix(t(assay(spe_cells, "logcounts"))))
spe_cells$cell_type_pred <- pred_cells

pred_tiles = predict(fit, newdata = as.matrix(t(assay(spe_tiles, "logcounts"))))
spe_tiles$cell_type_pred <- pred_tiles

Plots

Generate plots of cells/tiles in space coloured by predicted cell type

g = plotReducedDim(spe_cells, "spatial",
                   colour_by = "cell_type_pred", point_size = 0.5) + 
  ggtitle("cells") +
  scale_colour_manual(values = pancreas_colours) +
  coord_fixed() +
  plotReducedDim(spe_tiles, "spatial",
                 colour_by = "cell_type_pred", point_size = 0.5) + 
  ggtitle("tiles") + 
  scale_colour_manual(values = pancreas_colours) + 
  coord_fixed() +
  plot_layout(nrow = 2) +
  NULL
g

Save the objects

We save the two SpatialExperiment objects in the ../ProcessedData/ folder.

saveRDS(spe_cells, file = "../processedData/xenium_pancreas_spe_cells.Rds")
saveRDS(spe_tiles, file = "../processedData/xenium_pancreas_spe_tiles.Rds")

Finish

sessionInfo()
## R version 4.4.0 (2024-04-24)
## Platform: aarch64-apple-darwin23.4.0
## Running under: macOS Sonoma 14.7
## 
## Matrix products: default
## BLAS:   /opt/homebrew/Cellar/openblas/0.3.27/lib/libopenblasp-r0.3.27.dylib 
## LAPACK: /opt/homebrew/Cellar/r/4.4.0/lib/R/lib/libRlapack.dylib;  LAPACK version 3.12.0
## 
## locale:
## [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
## 
## time zone: Australia/Sydney
## tzcode source: internal
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] patchwork_1.3.0             scater_1.32.1              
##  [3] ggplot2_3.5.1               scuttle_1.14.0             
##  [5] randomForest_4.7-1.2        SpatialExperiment_1.14.0   
##  [7] SingleCellExperiment_1.26.0 SummarizedExperiment_1.34.0
##  [9] Biobase_2.64.0              GenomicRanges_1.56.2       
## [11] GenomeInfoDb_1.40.1         IRanges_2.38.1             
## [13] S4Vectors_0.42.1            BiocGenerics_0.50.0        
## [15] MatrixGenerics_1.16.0       matrixStats_1.4.1          
## [17] MoleculeExperiment_1.5.1   
## 
## loaded via a namespace (and not attached):
##  [1] bitops_1.0-9              gridExtra_2.3            
##  [3] rlang_1.1.4               magrittr_2.0.3           
##  [5] compiler_4.4.0            DelayedMatrixStats_1.26.0
##  [7] png_0.1-8                 fftwtools_0.9-11         
##  [9] vctrs_0.6.5               pkgconfig_2.0.3          
## [11] crayon_1.5.3              fastmap_1.2.0            
## [13] magick_2.8.5              XVector_0.44.0           
## [15] labeling_0.4.3            utf8_1.2.4               
## [17] rmarkdown_2.28            UCSC.utils_1.0.0         
## [19] ggbeeswarm_0.7.2          xfun_0.48                
## [21] zlibbioc_1.50.0           cachem_1.1.0             
## [23] beachmat_2.20.0           jsonlite_1.8.9           
## [25] highr_0.11                DelayedArray_0.30.1      
## [27] BiocParallel_1.38.0       jpeg_0.1-10              
## [29] tiff_0.1-12               terra_1.7-83             
## [31] irlba_2.3.5.1             parallel_4.4.0           
## [33] R6_2.5.1                  bslib_0.8.0              
## [35] jquerylib_0.1.4           Rcpp_1.0.13              
## [37] knitr_1.48                Matrix_1.7-0             
## [39] tidyselect_1.2.1          rstudioapi_0.16.0        
## [41] abind_1.4-8               yaml_2.3.10              
## [43] viridis_0.6.5             EBImage_4.46.0           
## [45] codetools_0.2-20          lattice_0.22-6           
## [47] tibble_3.2.1              withr_3.0.1              
## [49] evaluate_1.0.1            pillar_1.9.0             
## [51] generics_0.1.3            RCurl_1.98-1.16          
## [53] sparseMatrixStats_1.16.0  munsell_0.5.1            
## [55] scales_1.3.0              glue_1.8.0               
## [57] tools_4.4.0               BiocNeighbors_1.22.0     
## [59] data.table_1.16.2         ScaledMatrix_1.12.0      
## [61] locfit_1.5-9.10           cowplot_1.1.3            
## [63] grid_4.4.0                colorspace_2.1-1         
## [65] GenomeInfoDbData_1.2.12   beeswarm_0.4.0           
## [67] BiocSingular_1.20.0       vipor_0.4.7              
## [69] cli_3.6.3                 rsvd_1.0.5               
## [71] fansi_1.0.6               S4Arrays_1.4.1           
## [73] viridisLite_0.4.2         dplyr_1.1.4              
## [75] gtable_0.3.5              sass_0.4.9               
## [77] digest_0.6.37             SparseArray_1.4.8        
## [79] ggrepel_0.9.6             rjson_0.2.23             
## [81] htmlwidgets_1.6.4         farver_2.1.2             
## [83] htmltools_0.5.8.1         lifecycle_1.0.4          
## [85] httr_1.4.7